package com.omazan.facace;

import java.util.List;

import com.omazan.db.HandleUserTypes;
import com.omazan.db.HandleUsers;
import com.omazan.db.models.User;
import com.omazan.facace.models.LoginDetails;
import com.omazan.facace.models.UserSessionDetails;
import com.omazan.utils.Constants;
import com.omazan.utils.OmazanException;

public class ManageUserSession {

	public UserSessionDetails loginUser(String userName, String password)
			throws OmazanException {

		HandleUsers handleUsers = new HandleUsers();
		List<User> users = handleUsers.readAllUsersFromDB();
		UserSessionDetails userSessionDetails = new UserSessionDetails();

		for (User eachUser : users) {

			if (eachUser.isActive() && eachUser.getUserName().equals(userName)
					&& eachUser.getPassword().equals(password)) {
				LoginDetails loginDetails = new LoginDetails();
				loginDetails.setUser(eachUser.getUserName());
				loginDetails.setUserType(new HandleUserTypes()
						.readUserTypeFromDBUsingId(
								eachUser.getUserType().getUserTypeId())
						.getUserType());
				userSessionDetails.setLoginDetails(loginDetails);
				
				if(loginDetails.getUserType().equals(Constants.admin)){
					
					userSessionDetails.setContentToLoad(Constants.adminHome);
					userSessionDetails.setUserMenu(Constants.adminMenu);
				} else if(loginDetails.getUserType().equals(Constants.cust)){
					userSessionDetails.setContentToLoad(Constants.customerHome);
					userSessionDetails.setUserMenu(Constants.custMenu);
					
				} else {
					userSessionDetails.setContentToLoad(Constants.empHome);
					userSessionDetails.setUserMenu(Constants.empMenu);
				}
				
				return userSessionDetails;
			}
			
		}
		return null;

	}
}
